package com.lili.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.controller.request.*;
import com.lili.exception.QhException;
import com.lili.exception.QhUserErrors;
import com.lili.mapper.*;
import com.lili.po.*;
import com.lili.uitl.ShiroUtils;
import com.lili.vo.XasUserInfoRelationshipVo;
import com.lili.vo.XasUserInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;


@Service
public class XasUserInfoService {
    @Autowired
    XasUserAddressListMapper xasUserAddressListMapper;
    @Autowired
    XasUserInfoMapper xasUserInfoMapper;
    @Autowired
    XasUserPointRecordsMapper xasUserPointRecordsMapper;
    @Autowired
    XasUserBalanceRecordsMapper xasUserBalanceRecordsMapper;
    @Autowired
    XasUserBenefitsRecordsMapper xasUserBenefitsRecordsMapper;
    @Autowired
    XasUserCoinRecordsMapper xasUserCoinRecordsMapper;
    @Autowired
    XasUserBankcardListMapper xasUserBankcardListMapper;
    @Autowired
    XasUserAuthenticationRecordsMapper xasUserAuthenticationRecordsMapper;
    @Autowired
    XasUserRelationshipMapper xasUserRelationshipMapper;
    @Autowired
    XasVipPriceMapper xasVipPriceMapper;
    @Autowired
    XasVipPrivilegeMapper xasVipPrivilegeMapper;
    @Autowired
    XasUserShoppingCartListMapper xasUserShoppingCartListMapper;
    @Autowired
    XasMsgNotificationMapper xasMsgNotificationMapper;
    @Autowired
    XasMsgSystemMapper xasMsgSystemMapper;
    @Autowired
    XasSysConfigMapper xasSysConfigMapper;
    @Autowired
    UserService userService;

    //用户信息 按照姓名和手机号查询，可以修改vip等级
    public IPage<XasUserInfo> xasUserInfoList(XasUserInfoRequest xasUserInfoRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserInfoRequest, page);
        LambdaQueryWrapper<XasUserInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasUserInfoRequest.getPhone()), XasUserInfo::getPhone, xasUserInfoRequest.getPhone());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasUserInfoRequest.getNick()), XasUserInfo::getNick, xasUserInfoRequest.getNick());
        IPage<XasUserInfo> iPage = xasUserInfoMapper.selectPage(page, lambdaQueryWrapper);
        return iPage;
    }

    public List<XasUserInfo> xasUserRelationship(String phone, Integer type) {
        LambdaQueryWrapper<XasUserInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(XasUserInfo::getPhone, phone);
        XasUserInfo xasUserInfo = xasUserInfoMapper.selectOne(lambdaQueryWrapper);
        List<XasUserInfo> result = Collections.emptyList();
        if (xasUserInfo == null) {
            return result;
        }
        if (type == 1) {
            result = xasUserInfoMapper.xasUserRelationshipUP(xasUserInfo.getId());
        } else if (type == 2) {
            result = xasUserInfoMapper.xasUserRelationshipUPTwo(xasUserInfo.getId());
        } else if (type == 3) {
            result = xasUserInfoMapper.xasUserRelationshipDown(xasUserInfo.getId());
        } else if (type == 4) {
            result = xasUserInfoMapper.xasUserRelationshipDownTwo(xasUserInfo.getId());
        } else {
            return Collections.emptyList();
        }
        return result;
    }


    //收货地址 按照手机号和收货人姓名查询
    public IPage<XasUserAddressList> xasUserAddressList(XasUserAddressListRequest xasUserAddressListRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserAddressListRequest, page);
        LambdaQueryWrapper<XasUserAddressList> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasUserAddressListRequest.getPhone()), XasUserAddressList::getPhone, xasUserAddressListRequest.getPhone());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasUserAddressListRequest.getName()), XasUserAddressList::getName, xasUserAddressListRequest.getName());
        IPage<XasUserAddressList> iPage = xasUserAddressListMapper.selectPage(page, lambdaQueryWrapper);
        return iPage;
    }


    //积分记录 按用户手机号和时间段查询
    public IPage<XasUserPointRecords> xasUserPointRecordsListByPhone(XasUserPointRecordsRequest xasUserAddressListRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserAddressListRequest, page);
        IPage iPage = xasUserPointRecordsMapper.xasUserPointRecordsListByPhone(page, xasUserAddressListRequest.getStartTime(), xasUserAddressListRequest.getEndTime(), xasUserAddressListRequest.getPhone());
        return iPage;
    }


    //余额记录 按用户手机号和时间段查询
    public IPage<XasUserBalanceRecords> xasUserBalanceRecordsListByPhone(XasUserBalanceRecordsRequest xasUserBalanceRecordsRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserBalanceRecordsRequest, page);
        IPage iPage = xasUserBalanceRecordsMapper.xasUserBalanceRecordsListByPhone(page, xasUserBalanceRecordsRequest.getStartTime(), xasUserBalanceRecordsRequest.getEndTime(), xasUserBalanceRecordsRequest.getPhone());
        return iPage;
    }

    //收益记录 按用户手机号和时间段查询  xas_user_benefits_records

    public IPage<XasUserBenefitsRecords> xasUserBenefitsRecordsListByPhone(XasUserBenefitsRecordsRequest xasUserBenefitsRecordsRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserBenefitsRecordsRequest, page);
        IPage iPage = xasUserBenefitsRecordsMapper.xasUserBenefitsRecordsListByPhone(page, xasUserBenefitsRecordsRequest.getStartTime(), xasUserBenefitsRecordsRequest.getEndTime(), xasUserBenefitsRecordsRequest.getPhone());
        return iPage;
    }


    //平台币记录  按用户手机号和时间段查询
    public IPage<XasUserCoinRecords> xasUserCoinRecordsListByPhone(XasUserCoinRecordsRequest xasUserCoinRecordsRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserCoinRecordsRequest, page);
        IPage iPage = xasUserCoinRecordsMapper.xasUserCoinRecordsListByPhone(page, xasUserCoinRecordsRequest.getStartTime(), xasUserCoinRecordsRequest.getEndTime(), xasUserCoinRecordsRequest.getPhone());
        return iPage;
    }

    //银行卡  按照用户手机号查询
    public IPage<XasUserBankcardList> xasUserBankcardListByPhone(XasUserBankcardListRequest xasUserBankcardListRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserBankcardListRequest, page);
        IPage iPage = xasUserBankcardListMapper.xasUserBankcardListByPhone(page, xasUserBankcardListRequest.getPhone());
        return iPage;
    }

    //实名认证  按照手机号，姓名，状态查询
    public IPage<XasUserAuthenticationRecords> xasUserAuthenticationRecords(XasUserAuthenticationRecordsRequest xasUserAuthenticationRecordsRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserAuthenticationRecordsRequest, page);
        IPage iPage = xasUserAuthenticationRecordsMapper.xasUserAuthenticationRecords(page, xasUserAuthenticationRecordsRequest.getPhone(), xasUserAuthenticationRecordsRequest.getName(), xasUserAuthenticationRecordsRequest.getStatus());
        return iPage;
    }

    //用户关系 需要根据用户手机号查询用户的上下2级,通过选择其中一种查询，具体展示信息显示名称和手机号，vip等级等
    public XasUserInfoRelationshipVo xasUserInfoRelationshipVo(String phone) {
        XasUserInfoRelationshipVo xasUserInfoRelationshipVo = new XasUserInfoRelationshipVo();
        LambdaQueryWrapper<XasUserInfo> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(XasUserInfo::getPhone, phone);
        XasUserInfo xasUserInfo = xasUserInfoMapper.selectOne(lambdaQueryWrapper);
        if (xasUserInfo == null) {
            throw new QhException(QhUserErrors.USER_NOT_EXIST);
        }
        XasUserInfoVo xasUserInfoVo = new XasUserInfoVo();
        BeanUtils.copyProperties(xasUserInfo, xasUserInfoVo);
        xasUserInfoRelationshipVo.setXasUserInfoVo(xasUserInfoVo);
//        xasUserRelationshipMapper.
        return xasUserInfoRelationshipVo;
    }


    public IPage<XasVipPrice> selectXasVipPrice(XasVipPriceRequest xasVipPriceRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasVipPriceRequest, page);
        LambdaQueryWrapper<XasVipPrice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        IPage<XasVipPrice> iPage = xasVipPriceMapper.selectPage(page, lambdaQueryWrapper);
        return iPage;
    }

    public List<XasVipPrice> selectAllXasVipPrice() {
        return xasVipPriceMapper.selectList(null);
    }

    @Transactional
    public void addXasVipPrice(XasVipPrice xasVipPrice) {
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasVipPrice.setCreateUser(userLoginName);
        xasVipPrice.setCreateTime(new Date());
        xasVipPriceMapper.insert(xasVipPrice);
    }

    @Transactional
    public void updateXasVipPrice(XasVipPrice xasVipPrice) {
        xasVipPriceMapper.updateById(xasVipPrice);
    }

    @Transactional
    public void delXasVipPrice(Integer id) {
        xasVipPriceMapper.deleteById(id);
    }


    public IPage<XasVipPrivilege> selectXasVipPrivilege(XasVipPrivilegeRequest xasVipPrivilegeRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasVipPrivilegeRequest, page);
        IPage<XasVipPrivilege> iPage = xasVipPrivilegeMapper.selectXasVipPrivilege(page, xasVipPrivilegeRequest.getName());
        return iPage;
    }

    @Transactional
    public void addXasVipPrivilege(XasVipPrivilege xasVipPrivilege) {
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasVipPrivilege.setCreateUser(userLoginName);
        xasVipPrivilege.setCreateTime(new Date());
        xasVipPrivilegeMapper.insert(xasVipPrivilege);
    }

    @Transactional
    public void updateXasVipPrivilege(XasVipPrivilege xasVipPrivilege) {
        xasVipPrivilegeMapper.updateById(xasVipPrivilege);
    }

    @Transactional
    public void delXasVipPrivilege(Integer id) {
        xasVipPrivilegeMapper.deleteById(id);
    }

    @Transactional
    public void examineXasUserAuthenticationRecords(Integer id, Integer status) {
        XasUserAuthenticationRecords xasUserAuthenticationRecords = new XasUserAuthenticationRecords();
        xasUserAuthenticationRecords.setId(id);
        xasUserAuthenticationRecords.setStatus(status);
        xasUserAuthenticationRecordsMapper.updateById(xasUserAuthenticationRecords);

    }

    public IPage<XasUserShoppingCartList> selectXasUserShoppingCartList(XasUserInfoRequest xasUserInfoRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserInfoRequest, page);
        IPage<XasUserShoppingCartList> iPage = xasUserShoppingCartListMapper.selectXasVipPrivilege(page, xasUserInfoRequest.getPhone());
        return iPage;
    }

    public IPage<XasMsgNotification> selectXasMsgNotification(XasMsgNotificationRequest xasMsgNotificationRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasMsgNotificationRequest, page);
        LambdaQueryWrapper<XasMsgNotification> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasMsgNotificationRequest.getTitle()), XasMsgNotification::getTitle, xasMsgNotificationRequest.getTitle());
        IPage<XasMsgNotification> xasMsgNotificationIPage = xasMsgNotificationMapper.selectPage(page, lambdaQueryWrapper);
        return xasMsgNotificationIPage;
    }

    public IPage<XasMsgSystem> selectXasMsgSystem(XasMsgNotificationRequest xasMsgNotificationRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasMsgNotificationRequest, page);
        LambdaQueryWrapper<XasMsgSystem> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasMsgNotificationRequest.getTitle()), XasMsgSystem::getTitle, xasMsgNotificationRequest.getTitle());
        IPage<XasMsgSystem> xasMsgSystemIPage = xasMsgSystemMapper.selectPage(page, lambdaQueryWrapper);
        return xasMsgSystemIPage;
    }

    public void addXasMsgSystem(XasMsgSystem xasMsgSystem) {
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasMsgSystem.setCreateUser(userLoginName);
        xasMsgSystem.setCreateTime(new Date());
        xasMsgSystemMapper.insert(xasMsgSystem);
    }

    public void updateXasMsgSystem(XasMsgSystem xasMsgSystem) {
        xasMsgSystemMapper.updateById(xasMsgSystem);
    }

    public void delXasMsgSystem(Integer id) {
        xasMsgSystemMapper.deleteById(id);
    }

    public IPage<XasSysConfig> selectXasSysConfig(XasMsgNotificationRequest xasMsgNotificationRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasMsgNotificationRequest, page);
        IPage<XasSysConfig> xasSysConfigIPage = xasSysConfigMapper.selectPage(page, null);
        return xasSysConfigIPage;

    }

    public void addXasSysConfig(XasSysConfig xasSysConfig) {
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasSysConfig.setCreateUser(userLoginName);
        xasSysConfig.setCreateTime(new Date());
        xasSysConfigMapper.insert(xasSysConfig);
    }

    public void updateXasSysConfig(XasSysConfig xasSysConfig) {
        xasSysConfigMapper.updateById(xasSysConfig);
    }

    public void delXasSysConfig(Integer id) {
        xasSysConfigMapper.deleteById(id);
    }
}
